package com.szxy.backend.content.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.szxy.backend.content.feign.CommonContentFeignClient;
import com.szxy.backend.content.service.ContentCategoryService;
import com.szxy.pojo.TbContentCategory;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/9 21:29
 * @Description:com.szxy.backend.content.service.impl
 * @Version:1.0
 **/
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private CommonContentFeignClient commonContentFeignClient;

    @Override
    @LcnTransaction
    public Result deleteContentCategoryById(Long categoryId) {
        delContentCategoryById(categoryId);
        return Result.ok();
    }

    public void delContentCategoryById(Long categoryId){
        //查询当前节点下是否有子节点
        List<TbContentCategory> contentCategoryList =
                this.commonContentFeignClient.selectContentCategoryByParentId(categoryId);
        //若有子节点信息，递归删除子节点删除
        if (contentCategoryList != null && contentCategoryList.size() > 0) {
            //若当前节点是父节点，需要删除子节点以及子节点下的子节点信息
            for (TbContentCategory tbContentCategory : contentCategoryList) {
                delContentCategoryById(tbContentCategory.getId());
                this.commonContentFeignClient.deleteContentCategoryById(tbContentCategory.getId());
            }
        }
        //最后删除当前节点 categoryId
        this.commonContentFeignClient.deleteContentCategoryById(categoryId);
    }

    @Override
    @LcnTransaction
    public Result updateContentCategory(TbContentCategory contentCategory) {
        contentCategory.setUpdated(new Date());
        Integer flag = this.commonContentFeignClient.updateContentCategoryById(contentCategory);
        if(flag > 0){
            return Result.ok();
        }
        return Result.error("更新错误");
    }

    @Override
    @LcnTransaction
    public Result insertContentCategory(TbContentCategory contentCategory) {
        List<TbContentCategory> contentCategoryList =
                this.commonContentFeignClient.selectContentCategoryByParentId(contentCategory.getParentId());
        //查看当前新增节点的父节点下， 是否有其他子节点，若没有，则更新父节点信息
        if (contentCategoryList != null && contentCategoryList.size() == 0) {
            //创建 TbContentCategory 对象
            TbContentCategory contentCategoryParent = new TbContentCategory();
            contentCategoryParent.setId(contentCategory.getParentId());
            contentCategoryParent.setUpdated(new Date());
            contentCategoryParent.setIsParent(true);
            //更新父节点信息
            this.commonContentFeignClient.updateContentCategoryById(contentCategoryParent);
        }
        contentCategory.setIsParent(false);
        contentCategory.setStatus(1); //正常
        contentCategory.setSortOrder(1);//表示同级类目的展现次序，如数值相等则按名称次序排列。取值范围:大于零的整数',
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        int flag = this.commonContentFeignClient.insertContentCategory(contentCategory);
        if (flag > 0) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    @Override
    public Result selectContentCategoryByParentId(Long id) {
        try {
            List<TbContentCategory> tbContentCategoryList =
                    this.commonContentFeignClient.selectContentCategoryByParentId(id);
            return Result.ok(tbContentCategoryList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询错误");
    }
}
